package com.shuwen.gcdj.service.power;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.common.enums.DataLevelEnum;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.dto.power.SearchPowerApiDto;
import com.shuwen.gcdj.entity.PowerApi;
import com.shuwen.gcdj.mapper.mysql.PowerApiMapper;
import com.shuwen.gcdj.service.BaseService;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class PowerApiService extends BaseService<PowerApiMapper, PowerApi> {

    private final PowerApiMapper powerApiMapper;
    private final PowerApiAuthDeptService powerApiAuthDeptService;

    /**
     * 获取列表
     *
     * @param dto SearchPowerApiDto
     * @return List<PowerApi>
     */
    public List<PowerApi> searchList(SearchPowerApiDto dto) {
        return powerApiMapper.selectList(buildQueryWrapper(dto));
    }

    /**
     * 检查是否存在
     *
     * @param dto SearchRolePowerDto
     * @return boolean
     */
    public boolean checkExist(SearchPowerApiDto dto) {
        Integer c = powerApiMapper.selectCount(buildQueryWrapper(dto));
        return c != null && c >= 1;
    }

    /**
     * 物理删除权限Api关系
     *
     * @param dto         SearchRolePowerDto
     * @param initUserDto InitUserDto
     */
    public void deletePowerApi(SearchPowerApiDto dto, InitUserDto initUserDto) {
        if ((dto.getApiCodes() == null || dto.getApiCodes().isEmpty()) && (dto.getPowerCodes() == null || dto.getPowerCodes().isEmpty())) {
            return;
        }

        QueryWrapper<PowerApi> q = buildQueryWrapper(dto);
        List<PowerApi> results = powerApiMapper.selectList(q);
        List<String> delIds = new ArrayList<>();
        if (results != null && !results.isEmpty()) {
            List<String> delAuthDeptIds = new ArrayList<>();
            for (PowerApi item : results) {
                delIds.add(item.getId());
                item.setIsDelete(Constants.IS_DELETE_YES);
                if (initUserDto != null && initUserDto.getUserId() != null) {
                    item.setUpdateId(initUserDto.getUserId());
                }
                delAuthDeptIds.add(item.getId());
            }

            powerApiMapper.deleteBatchIds(delIds);

            // 删除Api维度授权部门id
            powerApiAuthDeptService.phyDeleteByApiCode(delAuthDeptIds);
        }
    }

    /**
     * 批量替换权限码
     *
     * @param powerCode    String
     * @param newPowerCode String
     */
    public void replacePowerCode(String powerCode, String newPowerCode) {
        if (powerCode != null && !powerCode.isEmpty() && newPowerCode != null && !newPowerCode.isEmpty()) {
            QueryWrapper<PowerApi> qw = new QueryWrapper<>();
            qw.eq("`power_code`", powerCode);
            qw.eq(Constants.FIELD_KEY_IS_DELETE, Constants.IS_DELETE_NORMAL);
            List<PowerApi> rws = powerApiMapper.selectList(qw);
            if (rws != null) {
                for (PowerApi rw : rws) {
                    rw.setPowerCode(newPowerCode);
                }
                updateBatchById(rws);
            }

        }
    }

    private QueryWrapper<PowerApi> buildQueryWrapper(SearchPowerApiDto dto) {
        QueryWrapper<PowerApi> q = new QueryWrapper<>();
        if (dto.getApiCodes() != null && !dto.getApiCodes().isEmpty()) {
            q.in("`api_code`", dto.getApiCodes());
        }

        if (dto.getIsDelete() != null && !dto.getIsDelete().isEmpty()) {
            q.in("`is_delete`", dto.getIsDelete());
        } else {
            q.eq(Constants.FIELD_KEY_IS_DELETE, Constants.IS_DELETE_NORMAL);
        }

        if (dto.getPowerCodes() != null && !dto.getPowerCodes().isEmpty()) {
            q.in("`power_code`", dto.getPowerCodes());
        }

        if (dto.getSettingType() != null && !dto.getSettingType().isEmpty()) {
            q.in("`setting_type`", dto.getSettingType());
        }

        return q;
    }

    /**
     * 权限码绑定Api
     *
     * @param id          t_user_power_api.id
     * @param initUserDto InitUserDto
     */
    public void unbindApi(String id, InitUserDto initUserDto) {
        if (id == null || id.isEmpty()) {
            return;
        }

        PowerApi one = getById(id);
        if (one == null) {
            return;
        }

        if (one.getDataLevel() != null && one.getDataLevel().equals(DataLevelEnum.DATA_LEVEL_SELECT_DEPT.getDataLevel())) {
            powerApiAuthDeptService.phyDeleteByApiCode(Lists.newArrayList(id));
        }

        // 删除主表
        powerApiMapper.deleteById(id);
    }
}
